package blip.jcaf.cm;

import java.rmi.RemoteException;

import blip.jcaf.cm.entity.Visitor;
import dk.pervasive.jcaf.util.AbstractContextClient;

public class ContextClient extends AbstractContextClient {
	
	private Visitor visitors[];

	public ContextClient(String serviceUri) {
		super(serviceUri);
		visitors = new Visitor[2];
		
		ping();
		load();
		test();
		run();
	}

    private void ping() {
        try {
            System.out.println("Server info: \n   " + getContextService().getServerInfo());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Adding entities to the ContextService
     */
    private void load() {
    	Driver driver = new Driver();
    	visitors = driver.getVisitors();
    	try {
			getContextService().addEntity(visitors[0]);
			getContextService().addEntity(visitors[1]);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    private void test() {
    	/*DisplayClient dc1 = new DisplayClient("blipTest", "itu.zone4.zone4e");
    	dc1.run();
    	DisplayClient dc2 = new DisplayClient("blipTest", "itu.zone4.zone4c");
    	dc2.run();
    	DisplayClient dc3 = new DisplayClient("blipTest", "itu.zone4.zone4d");
    	dc3.run();
    	DisplayClient dc4 = new DisplayClient("blipTest", "itu.zone4.zone4b");
    	dc4.run(); */
    	
    	
    	
    	
    /*	Runnable dc1 = new DisplayClient("blipTest", "itu.zone4.zone4b");
    	Thread t1 = new Thread(dc1);
    	t1.start();
    	//try { t1.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }

    	Runnable dc2 = new DisplayClient("blipTest", "itu.zone4.zone4c");
    	Thread t2 = new Thread(dc2);
    	t2.start();
    	//try { t2.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
    	
    	Runnable dc3 = new DisplayClient("blipTest", "itu.zone4.zone4d");
    	Thread t3 = new Thread(dc3);
    	t3.start();
    	//try { t3.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
    	
    	Runnable dc4 = new DisplayClient("blipTest", "itu.zone4.zone4e");
    	Thread t4 = new Thread(dc4);
    	t4.start();
    	//try { t4.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }*/
    }
    
	@Override
	public void run() {
		
	}

	public static void main(String[] args) {
		new ContextClient("blipTest");
		/*javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });*/
	}
}
